import { create } from 'zustand';

type State = {
    openKeys: string[];
    selectedMenuKeys: string[];
};

type Action = {
    setOpenKeys: (keys: string[]) => void;
    setSelectedMenuKeys: (menuKeys: string[]) => void;
    addOpenKey: (key: string) => void;
    clear: () => void;
};

/**
 * 菜单数据
 */
export const useMenuStore = create<State & Action>((set, get) => ({
    openKeys: [], // 展开的菜单key
    selectedMenuKeys: [], // 选中的菜单key

    // 新增openKeys
    addOpenKey: (key) => {
        if (!get().openKeys.includes(key)) {
            set({ openKeys: [...get().openKeys, key] });
        }
    },
    // 设置openKeys
    setOpenKeys: (keys) => {
        if (keys.toString() !== get().openKeys.toString()) {
            set({ openKeys: keys });
        }
    },
    // 设置selectedMenuKeys
    setSelectedMenuKeys: (menuKeys) => {
        if (menuKeys.toString() !== get().selectedMenuKeys.toString()) {
            set({ selectedMenuKeys: menuKeys });
        }
    },

    clear: () => {
        set({ openKeys: [], selectedMenuKeys: [] });
    },
}));
